﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace WebApplication.Controllers
{
    public class DataSetController : Controller
    {
        Models.DataClasses1DataContext d = new Models.DataClasses1DataContext();
        // GET: DataSet
        public ActionResult Index()
        {
         
        
            return View(   d.DataSet);
        }

    
        // GET: DataSet/Create
        public ActionResult Create()
        {
            return View(); 
        }

        // POST: DataSet/Create
        [HttpPost]
        public ActionResult Create(HttpPostedFileBase file,FormCollection collection)
        {
            try
            {
              
                var b = new byte[file.ContentLength];
                file.InputStream.Read(b, 0, b.Length);
                Models.DataSet set = new Models.DataSet()
                {
                    name = collection.GetValues("name")[0],
                    note = collection.GetValues("note")[0],

                    dateTime = DateTime.Now,
                    data=b
                };
              
                d.DataSet.InsertOnSubmit(set);
                d.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: DataSet/Edit/5
        public ActionResult Edit(int id)
        {
            try
            {
                return View(d.DataSet.First(t => t.id == id));
            }
            catch
            {
                return RedirectToAction("Index");
            }
           
        }

        // POST: DataSet/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {

               var v= d.DataSet.First(t => t.id == id);
                v.name = collection.GetValues("name")[0];
                v.note = collection.GetValues("note")[0];
              
                d.SubmitChanges();

            
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        // GET: DataSet/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
               
                d.DataSet.DeleteOnSubmit(d.DataSet.First(t => t.id == id));
                d.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
             
                return RedirectToAction("Index");
            }
        }

        // POST: DataSet/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            return Delete(id);
        }


        public FileContentResult GetFile(int id)
        {
            var dat = d.DataSet.First(t => t.id == id);
            return File(dat.data.ToArray(), "application/zip", dat.name+".zip");
        }

    }
}
